package com.isoft.c2team3service2.controller;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.dto.HotNewsDTO;
import com.isoft.c2team3service2.service.HotNewsService;
import jakarta.mail.Multipart;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/hot-news")
@RequiredArgsConstructor
public class HotNewsController {

    private final HotNewsService hotNewsService;

    // 1. 查询所有热点新闻（前3条）
    @GetMapping
    public ResponseEntity<List<HotNewsDTO>> getHotNews() {
        return ResponseEntity.ok(hotNewsService.getHotNewsList());
    }

    // 2. 根据ID查询单条新闻
    @GetMapping("/{id}")
    public ResponseEntity<HotNewsDTO> getHotNewsById(@PathVariable Long id) {
        return ResponseEntity.ok(hotNewsService.getHotNewsById(id));
    }

    // 3. 创建热点新闻（POST请求，请求体携带新闻数据）
    // 1. 添加热点新闻：返回“添加成功/失败”
    @PostMapping("/add")
    public ResponseData<Boolean> createHotNews(@ModelAttribute HotNewsDTO hotNewsDTO,
                                               @RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            boolean isSuccess = hotNewsService.createHotNews(hotNewsDTO , file);
            // 复用resultBoolean：自动返回“热点新闻添加成功”或“失败”
            return new BaseController().resultBoolean(isSuccess, "热点新闻添加");
        } catch (Exception e) {
            ResponseData<Boolean> errorResponse = new ResponseData<>();
            errorResponse.setErrorCode(500);
            errorResponse.setMsg("热点新闻添加失败：" + e.getMessage());
            errorResponse.setData(false);
            return errorResponse;
        }
    }

    // 4. 更新热点新闻（PUT请求，路径携带ID，请求体携带更新数据）
    @PutMapping("/{id}")
    public ResponseData<Boolean> updateHotNews(
            @PathVariable Long id,
            @ModelAttribute HotNewsDTO hotNewsDTO,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) {
        try {
            // 调用修改后的 Service 方法，获取 boolean 结果
            boolean isUpdateSuccess = hotNewsService.updateHotNews(id, hotNewsDTO ,file);
            // 复用 BaseController 的 resultBoolean 方法，返回统一响应
            return new BaseController().resultBoolean(isUpdateSuccess, "热点新闻更新");
        } catch (MybatisPlusException e) {
            // 捕获“新闻不存在”异常，返回明确失败提示
            ResponseData<Boolean> errorResponse = new ResponseData<>();
            errorResponse.setErrorCode(500);
            errorResponse.setMsg(e.getMessage()); // 如“热点新闻不存在，ID：1”
            errorResponse.setData(false);
            return errorResponse;
        } catch (Exception e) {
            // 捕获其他异常（如数据库异常）
            ResponseData<Boolean> errorResponse = new ResponseData<>();
            errorResponse.setErrorCode(500);
            errorResponse.setMsg("热点新闻更新失败：" + e.getMessage());
            errorResponse.setData(false);
            return errorResponse;
        }
    }

    // 5. 删除热点新闻（DELETE请求，路径携带ID）
    @DeleteMapping("/{id}")
    public ResponseData<Boolean> deleteHotNews(@PathVariable Long id) {
        try {
            boolean isSuccess = hotNewsService.deleteHotNews(id);
            // 复用resultBoolean：自动返回“热点新闻删除成功”或“失败”
            return new BaseController().resultBoolean(isSuccess, "热点新闻删除");
        } catch (MybatisPlusException e) {
            ResponseData<Boolean> errorResponse = new ResponseData<>();
            errorResponse.setErrorCode(500);
            errorResponse.setMsg(e.getMessage()); // 如“热点新闻不存在，ID：1”
            errorResponse.setData(false);
            return errorResponse;
        } catch (Exception e) {
            ResponseData<Boolean> errorResponse = new ResponseData<>();
            errorResponse.setErrorCode(500);
            errorResponse.setMsg("热点新闻删除失败：" + e.getMessage());
            errorResponse.setData(false);
            return errorResponse;
        }
    }
}